package it.univpm.diiga.semanticsearch.ricerca;
import java.io.*;
import java.net.*;
public class Download{

	private String error="";
	
	public String errorReport(){
		return this.error;
	}
	
	private void setError(String error){
		this.error=error;
	}
	
	public void save(String remoteFile,String localFile, String localPath){
		try{
			 
			if (!new File(localPath).exists())
				new File(localPath).mkdirs();
			
			/*Configuration.findLocalConfiguration();
			if (Configuration.isSetProxy()){
				System.getProperties().put( "proxySet", "true" );
  				System.getProperties().put( "proxyHost", Configuration.getConfiguration().get("proxyAddress"));
  				System.getProperties().put( "proxyPort", Configuration.getConfiguration().get("proxyPort"));
			}*/
			
			java.io.BufferedInputStream in = new java.io.BufferedInputStream(new java.net.URL(remoteFile).openStream());
			java.io.FileOutputStream fos = new java.io.FileOutputStream(localPath+localFile);
			java.io.BufferedOutputStream bout = new BufferedOutputStream(fos,2048);
			byte[] data = new byte[2048];
			int x=0;
			while((x=in.read(data,0,2048))>=0){
				bout.write(data,0,x);
			}
				bout.close();
				in.close();
		
	    }catch (IOException e){
	    	e.printStackTrace();
	    	setError("I/O error");
	    	System.out.println("I/O error");
	    	
	    } catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	 }
}